/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.search.model;

import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;

/**
 * Maintains a cache of fixed size for searches.
 *
 * @author Walter L�tgenau
 */
public class SearchCache {
    public static final int MAX_SEARCH_ENTRY = 10;
    public static final String ATT_SEARCH_CACHE = "searchCache";

    SearchEntry[] searches;
    int oldest = 0;

    public SearchCache() {
        this.searches  = new SearchEntry[MAX_SEARCH_ENTRY];
        for (int i = 0; i < MAX_SEARCH_ENTRY; i++)
            searches[i] = new SearchEntry();
    }

    public static SearchCache getInstance(HttpServletRequest request) {
        SearchCache instance = (SearchCache) request.getSession().getAttribute(ATT_SEARCH_CACHE);
        if (instance == null) {
            synchronized (SearchCache.class) {
                instance = (SearchCache) request.getSession().getAttribute(ATT_SEARCH_CACHE);
                if (instance == null) {
                    instance = new SearchCache();
                    request.getSession().setAttribute(ATT_SEARCH_CACHE, instance);
                }
            }
        }

        return instance;
    }

    /**
     * Return the search, that corresponds to the key.
     *
     * @param key
     * @return
     */
    public SearchEntry getSearch(String key) {
        int id;
        try {
            id = Integer.parseInt(key);
        } catch(NumberFormatException e) {
            return null;
        }

        return searches[id];
    }

    /**
     * Add a search to the cache. Try to find a free slot first, in
     * ascending order. If found, use that.
     *
     * If there is no free slot, use the oldest entry.
     * 
     * @param type
     * @param value
     * @return
     */
     public synchronized String addSearch(SearchType type, String value) {
        for (int i = oldest; i < MAX_SEARCH_ENTRY; i++) {
            if (searches[i].getValue() == null) {
                searches[i].setType(type);
                searches[i].setValue(value);

                return new Integer(i).toString();
            }
        }

       for (int i = 0; i < oldest; i++) {
            if (searches[i].getValue() == null) {
                searches[i].setType(type);
                searches[i].setValue(value);

                return new Integer(i).toString();
            }
        }

        searches[oldest].setType(type);
        searches[oldest].setValue(value);

        String key = new Integer(oldest).toString();
        oldest++;

        return key;
    }
}
